﻿using System;
using Eto.Parse.Grammars;

namespace Furesoft.Database
{
    class Program
    {
        static void Main(string[] args)
        {
            const string sqlStatement = @"SELECT * FROM MyTable WHERE MyColumn = hello";

            var ebnfGrammar = new EbnfGrammar(EbnfStyle.W3c | EbnfStyle.SquareBracketAsOptional | EbnfStyle.WhitespaceSeparator);

            var Grammar = ebnfGrammar.Build(sqlW3cEbnf, "command");
            var match = Grammar.Match(sqlStatement);

            foreach (var item in match.Matches)
            {
                foreach (var mm in item.Matches)
                {
                    var st = SyntaxTree.Build(mm);
                }
            }

            Console.ReadLine();
        }

        const string sqlW3cEbnf = @"command ::= define_data
| modify_data
| system_command
| user_command
define_data ::= create_command
modify_data ::= select_command
| insert_command
| update_command
| delete_command
system_command ::=
describe_command
| open_command
| close_command
| save_command
| list_command
| clear_command
| dump_command
| help_command

user_command ::= create_user

create_user ::= (""CREATE_USER"" | ""create_user"") (string) (string)

string ::= (character_set)+

operator ::= [""<""|"">""|""!""]""=""|[""<""|"">""|""=""]

create_command ::=
( ""CREATE"" | ""create"" ) (string)
""(""((string)"","")+"")""
(""WITH""|""with"") (""VALUES""|""values"")
""(""((string)"","")+"")""
select_command ::=
(""SELECT""|""select"")
(""*""|(string))
(""FROM""|""from"")
(string)
[(""WHERE""|""where"") condition]
[(""LIMIT""|""limit"") (numeric)+]
insert_command ::=
( ""INSERT"" | ""insert"" )
(""INTO""|""into"")
(string)
""(""((string)"","")+"")""
(""VALUES""|""values"")
""(""((string)"","")+"")""
update_command ::=
(""UPDATE""|""update"")
(string)
(""SET""|""set"")
(string)
""=""
(string)
[(""WHERE""|""where"") condition]
delete_command ::=
(""DELETE""|""delete"")
(""FROM""|""from"")
(string)
[(""WHERE""|""where"") condition]
open_command ::=
(""OPEN""|""open"")
(string)
close_command ::=
(""CLOSE""|""close"")
(string)
save_command ::=
(""SAVE""|""save"")
| (""SAVE""|""save"")
(""AS""|""as"")
(string)
list ::=
(""LS""|""ls""|""DIR""|""dir"")
| (""LS""|""ls""|""DIR""|""dir"")
(string)
clear ::=
(""CLEAR""|""clear""|""CLS""|""cls"")
dump ::=
(""DUMP""|""dump"")
help ::=
(""help""|""HELP""|""/HELP""|""/help""|""/?"")
condition ::=
(string)
(operator)
(string)
[((""AND""|""and"")|(""OR""|""or""))
(string)
(operator)
(string)]
character_set ::=
""a""|""b""|""c""|""d""|""e""|""f""|""g""|""h""|""i""|""j""|""k""|""l""|""m""
""n""|""o""|""p""|""q""|""r""|""s""|""t""|""u""|""v""|""w""|""x""|""y""|""z""
|""A""|""B""|""C""|""D""|""E""|""F""|""G""|""H""|""I""|""J""|""K""|""L""|""M""
|""N""|""O""|""P""|""Q""|""R""|""S""|""T""|""U""|""V""|""W""|""X""|""Y""|""Z""
|""0""|""1""|""2""|""3""|""4""|""5""|""6""|""7""|""8""|""9""
|""-""|""_""|""(""|"")""|"".""|"",""|'""'|""/""|""\""|""@""|""#""|""~""|""|""
|""*""|""&""|""%""|""$""|""£""|""!""|""+""|""?""|"">""|""<""|""=""
numeric ::=
""0""|""1""|""2""|""3""|""4""|""5""|""6""|""7""|""8""|""9""";
    }
}